package com.example.mybatisplus.controller;

import com.example.mybatisplus.model.domain.DictData;
import com.example.mybatisplus.service.SingleTimeService;
import com.example.mybatisplus.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.VisitResultService;
import com.example.mybatisplus.model.domain.VisitResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2022-05-29
 * @version v1.0
 */

@Controller
@RequestMapping("/api/visitResult")
public class VisitResultController {

    private final Logger logger = LoggerFactory.getLogger( VisitResultController.class );

    @Autowired
    private VisitResultService visitResultService;
    @Autowired
    private UserService userService;
    @Autowired
    private SingleTimeService singleTimeService;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        VisitResult  visitResult =  visitResultService.getById(id);
        return JsonResponse.success(visitResult);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        visitResultService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateVisitResult(VisitResult  visitResult) throws Exception {
        visitResultService.updateById(visitResult);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建VisitResult
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(VisitResult  visitResult) throws Exception {
        visitResultService.save(visitResult);
        return JsonResponse.success(null);
    }

    @RequestMapping(value = "/getdiseaseResuldict", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getdiseaseResuldict()throws Exception {
        List<DictData> dictData=visitResultService.getdiseaseResuldict();
        return JsonResponse.success(dictData);
    }



    /**
     * 描述：初访师记录结果并填入数据库(传入学生stu_id，初访时间visit_time(varchar))+++
     * */
    @RequestMapping(value = "/addvisitRessult", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse addvisitRessult(@RequestBody VisitResult visitResult) throws Exception {
        Subject subject= SecurityUtils.getSubject();

        String message= visitResultService.addvisitRessult(visitResult,subject.getPrincipal().toString());

        if(message=="添加成功"){
            return JsonResponse.success(message);
        }
        else return JsonResponse.failure(message);

    }


    /**
     *描述：查询学生自己的初访结果+++
     * */
    @RequestMapping(value = "/getresultbystuId/{username}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getresultbystuId(@PathVariable("username") String stuusername) throws Exception {
        List<VisitResult> visitResults=new ArrayList<>();
        if (visitResultService.getresultbystuId(stuusername)==null){
            return JsonResponse.failure("查询失败，结果未出");
        }
        else {
            visitResults=visitResultService.getresultbystuId(stuusername);
            return JsonResponse.success().setData(visitResults).setMessage("查询成功");
        }
    }

    /**
     *描述：批量删除初访结果
     */

    @RequestMapping(value = "/deleteresultFaker", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse deleteresultFaker(@RequestBody List<VisitResult> visitResults) throws Exception {
        List<Long> t = new ArrayList<>();
        for (VisitResult v : visitResults){
            t.add(v.getId());
        }
        visitResultService.removeByIds(t);

        return JsonResponse.success().setMessage("批量删除成功");
    }


    @RequestMapping(value = "/getall", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getall()throws Exception {
        List<VisitResult> visitResults =visitResultService.getall();
        List<Map<String,String>> list=new ArrayList<>();
        for (VisitResult visitResult:visitResults){
            list.add(packgeVisitResult(visitResult));
        }
        return JsonResponse.success().setData(list);
    }


    public Map<String,String> packgeVisitResult(VisitResult visitResult){
        Map<String,String> map=new HashMap<String, String>();
        try {
            map.put("stu_name",userService.getById(visitResult.getStuId()).getStuName());
            map.put("tea_name",userService.getConsultantById(visitResult.getTeaId()).getStuName());
            map.put("id",visitResult.getId().toString());
            map.put("teaId",visitResult.getTeaId().toString());
            map.put("stuId",visitResult.getStuId().toString());
            map.put("visitResult",visitResult.getVisitResult().toString());
            map.put("disease_level",visitResult.getDisease_level());
            map.put("disease_type",visitResult.getDisease_type());
        } catch (Exception e){
            return map;
        }
//        map.put("consultantId",consultMerge.getConsultantId().toString());
//        map.put("startDate",consultMerge.getStartDate().toString());
//        map.put("endDate",consultMerge.getEndDate().toString());
        return map;
    }

}

